package com.sou.technologytool.ui.login;

import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.benben.commoncore.utils.StatusBarUtils;
import com.benben.commoncore.utils.StringUtils;
import com.benben.commoncore.utils.StyledDialogUtils;
import com.benben.commoncore.utils.ToastUtils;
import com.google.gson.Gson;
import com.sou.technologytool.MyApplication;
import com.sou.technologytool.R;
import com.sou.technologytool.api.Api;
import com.sou.technologytool.api.ApiConfig;
import com.sou.technologytool.api.TtitCallback;
import com.sou.technologytool.base.BaseActivity;
import com.sou.technologytool.entity.NoticeInfoResponse;
import com.sou.technologytool.ui.MainActivity;

import java.util.HashMap;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.edt_phone)
    EditText edtPhone;
    @BindView(R.id.edt_code)
    TextView edtCode;
    @BindView(R.id.ll_code)
    RelativeLayout ll_code;
    @BindView(R.id.tv_code)
    TextView tv_code;

    private String name = "";
    private String leve = "";
    private String type = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {
        StatusBarUtils.fullScreen(this, getStatusBarColor());
        StatusBarUtils.setAndroidNativeLightStatusBar(mContext, needStatusBarDarkText());
        updateView();
//        Log.i("Unnamed-iiii","rd:" + rd());
    }

    private void updateView() {
        edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    edtPhone.setBackgroundResource(R.drawable.et_login_select_frame);
                } else {
                    edtPhone.setBackgroundResource(R.drawable.et_login_no_select_frame);
                }
            }
        });
        edtCode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    ll_code.setBackgroundResource(R.drawable.et_login_select_frame);
                } else {
                    ll_code.setBackgroundResource(R.drawable.et_login_no_select_frame);
                }
            }
        });
    }

    @OnClick({R.id.tv_login, R.id.tv_code})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.tv_login) {
            login();
        }
        if (view.getId() == R.id.tv_code) {
            sndMsg();
        }
    }

    private void sndMsg() {
        String phone = edtPhone.getText().toString().trim();
        if (StringUtils.isEmpty(phone)) {
            ToastUtils.show(mContext, "please input phone number");
            return;
        }
        StyledDialogUtils.getInstance().loading(mContext);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                edtCode.setText("0215");
                StyledDialogUtils.getInstance().dismissLoading();
            }
        }, 1200);
    }

    private void login() {
        String phone = edtPhone.getText().toString().trim();
        String otp = edtCode.getText().toString().trim();
        if (StringUtils.isEmpty(phone)) {
            ToastUtils.show(mContext, "please input phone number");
            return;
        }
        if (StringUtils.isEmpty(otp)) {
            ToastUtils.show(mContext, "please input OTP");
            return;
        }
        getLoginInfo(phone);
    }

    private void getLoginInfo(String phone) {
        StyledDialogUtils.getInstance().loading(mContext);
        HashMap<String, Object> params = new HashMap<>();
        params.put("user_name", phone);
        Api.config(ApiConfig.QUERY_BYTYPE, params).getRequest(this, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                StyledDialogUtils.getInstance().dismissLoading();
                Gson gson = new Gson();
                NoticeInfoResponse response = gson.fromJson(res, NoticeInfoResponse.class);
                if (response.getCode() == 1) {
                    name = response.getUser_name();
                    leve = response.getUserlevel();
                    type = response.getType();
                    MyApplication.mPreferenceProvider.setUserName(name);
                    MyApplication.mPreferenceProvider.setLevel(leve);
                    MyApplication.mPreferenceProvider.setUserType(type);
                    MyApplication.mPreferenceProvider.setIsLogin(true);
                    MyApplication.openActivity(mContext, MainActivity.class);
                }
                finish();
            }

            @Override
            public void onFailure(Exception e) {
                StyledDialogUtils.getInstance().dismissLoading();
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
